package com.qianya.system.manage.web.sys.controller;

import com.qianya.model.common.CommonResult;
import com.qianya.model.common.ResultCode;
import com.qianya.system.manage.web.sys.model.RedisInfo;
import com.qianya.system.manage.web.sys.service.RedisService;
import com.sun.management.OperatingSystemMXBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/sys/redis")
@Api(value = "信息监控controller", tags = "信息监控接口")
public class ActuatorRedisController {

	@Autowired
	private RedisService redisService;

	/**
	 * Redis详细信息
	 * @return
	 * @throws Exception
	 */
	@ApiOperation("查询redis详细信息")
	@GetMapping(value = "/info")
	public CommonResult getRedisInfo() throws Exception {
		try {
			List<RedisInfo> infoList = this.redisService.getRedisInfo();
			return CommonResult.success(infoList);
		} catch (Exception e) {
			return CommonResult.error(ResultCode.ERROR);
		}
	}

	@ApiOperation("查询redis key实时数量")
	@GetMapping(value = "/keysSize")
	public Map<String, Object> getKeysSize() throws Exception {
		return redisService.getKeysSize();
	}

	@ApiOperation("查询redis内存实时情况")
	@GetMapping(value = "/memoryInfo")
	public Map<String, Object> getMemoryInfo() throws Exception {
		return redisService.getMemoryInfo();
	}

	/**
	 * @Description: 查询磁盘信息
	 * @Param: [request, response]
	 * @return: java.lang.Object
	 * @Author: LHL
	 * @Date: 2020/6/02 12:50
	 */
	@ApiOperation("查询磁盘信息")
	@GetMapping(value = "/queryDiskInfo")
	public CommonResult queryDiskInfo(HttpServletRequest request, HttpServletResponse response){
		try {
			// 当前文件系统类
			FileSystemView fsv = FileSystemView.getFileSystemView();
			// 列出所有windows 磁盘
			File[] fs = File.listRoots();
			List<Map<String,Object>> list = new ArrayList<>();

			for (int i = 0; i < fs.length; i++) {
				if(fs[i].getTotalSpace()==0) {
					continue;
				}
				Map<String,Object> map = new HashMap<>();
				map.put("name", fsv.getSystemDisplayName(fs[i]));
				map.put("max", fs[i].getTotalSpace());
				map.put("rest", fs[i].getFreeSpace());
				map.put("restPPT", (fs[i].getTotalSpace()-fs[i].getFreeSpace())*100/fs[i].getTotalSpace());
				list.add(map);
			}
			return CommonResult.success(list);
		} catch (Exception e) {
			return CommonResult.error(ResultCode.ERROR);
		}
	}

	@SuppressWarnings("restriction")
	@ApiOperation("查询内存信息")
	@GetMapping(value = "/getMemInfo")
	public CommonResult getMemInfo() {
		Map<String,Double> map = new HashMap<>();
		OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
		double totalMemorySize = osmxb.getTotalPhysicalMemorySize();
		double freeMemorySize = osmxb.getFreePhysicalMemorySize();
		double usage = (totalMemorySize-freeMemorySize)/totalMemorySize;
		map.put("total", totalMemorySize);
		map.put("free", freeMemorySize);
		map.put("usage", usage);
		return CommonResult.success(map);
	}
}
